package com.loop.netty.inboundandoutbound;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * 使用netty搭建一个服务端
 * @author zhangxiaolu
 * @since 2023-09-22 22:47
 */
public class MyServer {

    public static void main(String[] args) {
        // boss和 worker EventLoopGroup
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            // ServerBootStrap创建
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            // 将两个EventLoopGroup设置到ServerBootStrap
            serverBootstrap.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new MyServerInitializer());

            // 绑定8080端口
            serverBootstrap.bind(8080).sync().channel().closeFuture().sync();

        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}
